package com.smm.web.vmc.service.impl;

import com.smm.web.vmc.dao.BaseDao;
import com.smm.web.vmc.domain.Admin;
import com.smm.web.vmc.domain.User;
import com.smm.web.vmc.service.AdminService;
import com.smm.web.vmc.util.MD5Util;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by zhenghao on 2016/9/6.
 *
 *
 */
@Service
public class AdminServiceImpl implements AdminService {


    @Resource
    private BaseDao<Admin> adminBaseDao;


    @Override
    public Admin login(String account, String password) {

        password = MD5Util.parseStrToMd5L32(password);

        DetachedCriteria where = DetachedCriteria.forClass(Admin.class);
        where.add(Restrictions.eq("account", account));
        where.add(Restrictions.eq("password", password));

        List<Admin> admins = adminBaseDao.query(where);

        if(admins==null || admins.size()==0) return null;

        Admin admin = admins.get(0);

        //登陆成功，记录最后登陆时间
        admin.setLastLoginTime(new Date());

        adminBaseDao.update(admin);

        return admin;
    }

    @Override
    public void update(Admin admin) throws Exception{

        adminBaseDao.update(admin);
    }
}
